package form3;

import org.apache.wicket.markup.html.WebPage;

	import java.io.IOException;
	import java.io.Reader;

	import java.sql.SQLException;
	import java.util.Arrays;
	import java.util.List;


	import org.apache.wicket.markup.html.WebPage;
	import org.apache.wicket.markup.html.form.Form;
	import org.apache.wicket.model.Model;
	import org.apache.wicket.markup.html.form.DropDownChoice;
	import org.apache.wicket.markup.html.form.TextField;
	import org.apache.wicket.markup.html.form.Button;


	//import org.apache.ibatis.io.Resources;
	//import org.apache.ibatis.session.SqlSession;
	//import org.apache.ibatis.session.SqlSessionFactory;
	//import org.apache.ibatis.session.SqlSessionFactoryBuilder;

	import FMS.Form3;



	import com.ibatis.common.resources.Resources;
	import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

	public class EditForm3 extends WebPage{
		
		//Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		//SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
		//SqlSession session = sqlMapper.openSession();
		
		Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		
		private Model rcaFormNoModel = new Model();
		private Model dateRequestedModel = new Model();
		private Model nameOfRequestingOfficerModel = new Model();
		private Model positionModel = new Model();
		private Model purposeModel = new Model();
		private Model specificActivitiesModel = new Model();
		private Model dateOfActivitiesModel = new Model();
		private Model approvedAmountModel= new Model();
		private Model statusModel = new Model();
		private DropDownChoice status;
		private List choice = Arrays.asList(new String[] { "PENDING", "APPROVED" });
		
			
		public EditForm3() throws IOException, SQLException {
			
			Form form = new Form("editForm3");
			
			TextField rcaFormNo = new TextField("rcaFormNo", rcaFormNoModel);
			rcaFormNo.setRequired(true);
			form.add(rcaFormNo);
			
			TextField dateRequested = new TextField("dateRequested", dateRequestedModel);
			dateRequested.setRequired(true);
			form.add(dateRequested);
			

			TextField nameOfRequestingOfficer = new TextField("nameOfRequestingOfficer", nameOfRequestingOfficerModel);
			nameOfRequestingOfficer.setRequired(true);
			form.add(nameOfRequestingOfficer);
			

			TextField position = new TextField("position", positionModel);
			position.setRequired(true);
			form.add(position);
			

			TextField purpose = new TextField("purpose", purposeModel);
			purpose.setRequired(true);
			form.add(purpose);
			

			TextField specificActivities = new TextField("specificActivities", specificActivitiesModel);
			specificActivities.setRequired(true);
			form.add(specificActivities);

			TextField dateOfActivities = new TextField("dateOfActivities", dateOfActivitiesModel);
			dateOfActivities.setRequired(true);
			form.add(dateOfActivities);
			
			TextField approvedAmount = new TextField("approvedAmount", approvedAmountModel);
			approvedAmount.setRequired(true);
			form.add(approvedAmount);
		
			status = new DropDownChoice("status", statusModel, choice);
			form.add(status);
			
			form.add(new Button("add"){
				
				private static final long serialVersionUID = 1L;
			
					public void onSubmit(){
					
						try {
					
							String rcaFormNo= (String)rcaFormNoModel.getObject();
							String dateRequested= (String)dateRequestedModel.getObject();
							String nameOfRequestingOfficer = (String)nameOfRequestingOfficerModel.getObject();
							String position = (String)positionModel.getObject();
							String purpose = (String)purposeModel.getObject();
							String specificActivities = (String)specificActivitiesModel.getObject();
							String dateOfActivities = (String)dateOfActivitiesModel.getObject();
						    String approvedAmount = (String)approvedAmountModel.getObject();
							String status = (String)statusModel.getObject();
		                    
							Form3 form3 = new Form3();
							
							form3.setRcaFormNo(Integer.parseInt(rcaFormNo));
							form3.setDateRequested(dateRequested);	
							form3.setNameOfRequestingOfficer(nameOfRequestingOfficer);
							form3.setPosition(position);
							form3.setPurpose(purpose);			
							form3.setSpecificActivities(specificActivities);
							form3.setDateOfActivities(dateOfActivities);
							form3.setApprovedAmount(Integer.parseInt(approvedAmount));
							form3.setStatus(status);
													
							
							sqlMap.insert("Form3.insert", form3);
							
							ViewForm3 page = new ViewForm3();
							setResponsePage(page);
							error("added");
						}
						catch (Exception exception){
						 	error("Fill up necessary fields");
					 	        exception.printStackTrace();}
					//}catch (IOException ioException) {
						//ioException.printStackTrace(); }
						
					}}); 
						
			//add(new FeedbackPanel("error"));
					
			add(form);
		
	}
	}


